<html>
<head><meta charset="utf-8"><title>Impacts of optimization on debug information · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html">Impacts of optimization on debug information</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228301283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228301283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nam Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228301283">(Mar 01 2021 at 20:34)</a>:</h4>
<p>Hello,</p>
<p>I am working on improving the debugging experience for Rust programs with variable shadowing in WinDbg. As WinDbg relies on scoping information to return the correct value of a shadowed variable, I need to understand how optimizations performed by rustc may affect debug information regarding relationship between lexical blocks. For instance, with this program:</p>
<div class="codehilite"><pre><span></span><code>#[inline(always)]
pub fn foo(first_var: u32) -&gt; u32 {
    let mut res = 0;
    res += first_var;
    println!(&quot;{}&quot;, first_var);
    {
        let first_var = 1;
        res += first_var;
        println!(&quot;{}&quot;, first_var);
    }
    res
}

fn main() {
    foo(0);
}
</code></pre></div>
<p>I got  this debug information, for all opt-level's:</p>
<div class="codehilite"><pre><span></span><code>[image.png](/user_uploads/4715/Yj9EuFzTS-iRcW30XjfumSe7/image.png) (0000A0)  S_BLOCK32: [0001:0000009F], Cb: 00000136,
          Parent: 0000004C, End: 00000194

(0000B8)   S_LOCAL: Local: 00000074, first_var
(0000CC)   S_DEFRANGE_FRAMEPOINTER_REL: FrameOffset: 005C
    Range: [0001:0000009F] - [0001:000001D5], 0 Gaps

….

(000118)   S_BLOCK32: [0001:0000013B], Cb: 0000009A,
           Parent: 000000A0, End: 00000190

(000130)    S_LOCAL: Local: 00000074, first_var
(000144)    S_DEFRANGE_FRAMEPOINTER_REL: FrameOffset: 00AC
    Range: [0001:0000013B] - [0001:000001D5], 0 Gaps

I wonder if rustc optimizations are always debug-info preserving? Is there any instance where debug information regarding scoping structure is altered by an optimization?
</code></pre></div>



<a name="228305626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228305626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228305626">(Mar 01 2021 at 21:03)</a>:</h4>
<p>No, optimisations don't preserve debug info. One instance where scoping information could be lost is if rustc deleted all the code in the scope if it deemed the scope to be dead code.</p>



<a name="228332624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228332624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228332624">(Mar 02 2021 at 00:27)</a>:</h4>
<p>That being said, AFAIK at MIR level all optimizations enabled by default preserve the debuginfo, so it should reach the LLVM at least. For example, <code>debug_assert_eq!</code> with debug assertions disabled generates debuginfo for <code>left_val</code> and <code>right_val</code>.</p>



<a name="228365119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228365119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228365119">(Mar 02 2021 at 07:24)</a>:</h4>
<p><code>debug_assert_eq!</code> with debug assertions disabled doesn't generate any debuginfo as it expands to nothing.</p>



<a name="228378210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228378210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228378210">(Mar 02 2021 at 09:25)</a>:</h4>
<p>That would generate warnings about functions that are only used from debug assertions, etc.</p>



<a name="228378611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228378611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228378611">(Mar 02 2021 at 09:27)</a>:</h4>
<p>Instead all code is there, but is preceded by <code>if false { ... }</code>.</p>



<a name="228823297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228823297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nam Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228823297">(Mar 04 2021 at 18:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Impacts.20of.20optimization.20on.20debug.20information/near/228305626">said</a>:</p>
<blockquote>
<p>No, optimisations don't preserve debug info. One instance where scoping information could be lost is if rustc deleted all the code in the scope if it deemed the scope to be dead code.</p>
</blockquote>
<p>So I compiled this program with opt-level=3:</p>
<div class="codehilite"><pre><span></span><code>pub fn foo(first_var: u32) -&gt; u32 {
    println!(&quot;{}&quot;, first_var);
    if (false) {
        let first_var = 5;
        println!(&quot;{}&quot;, first_var);
    }
    res
}

fn main() {
    foo(0);
}
</code></pre></div>
<p>And here is the debug info that I got in CodeView format:</p>
<div class="codehilite"><pre><span></span><code>(0000A0)  S_BLOCK32: [0001:0000009F], Cb: 00000136,
          Parent: 0000004C, End: 00000194

(0000B8)   S_LOCAL: Local: 00000074, first_var
(0000CC)   S_DEFRANGE_FRAMEPOINTER_REL: FrameOffset: 005C
    Range: [0001:0000009F] - [0001:000001D5], 0 Gaps

...

(000118)   S_BLOCK32: [0001:0000013B], Cb: 0000009A,
           Parent: 000000A0, End: 00000190

(000130)    S_LOCAL: Local: 00000074, first_var
(000144)    S_DEFRANGE_FRAMEPOINTER_REL: FrameOffset: 00AC
    Range: [0001:0000013B] - [0001:000001D5], 0 Gaps
</code></pre></div>
<p>Shouldn't the scope within the if statement considered dead-code? If so, why the block structure is still preserved?</p>



<a name="228824076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228824076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228824076">(Mar 04 2021 at 18:22)</a>:</h4>
<p>I can't read codeview dumps at all, sorry. Are you sure you're looking at the right output? That code does not compile and debug information about the <code>if(false)</code> block is gone at LLVM level.</p>



<a name="228824455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228824455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228824455">(Mar 04 2021 at 18:24)</a>:</h4>
<p>Hm, I guess the debug info itself is not necessarily gone, but its definitely can't be associated with any machine code, because said machine code simply doesn't exist.</p>



<a name="228826917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228826917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228826917">(Mar 04 2021 at 18:40)</a>:</h4>
<p>I messed with the example a bit but I don't think it fundamentally changes it. I mostly just wanted to get rid of the junk generated from using <code>println!()</code>. </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">print_call</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">first_var</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">print_call</span><span class="p">(</span><span class="n">first_var</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">first_var</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">print_call</span><span class="p">(</span><span class="n">first_var</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">first_var</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Compiling with <code>-O -g --emit llvm-ir</code>, it looks like the scoping information is still retained even for the dead code. (<a href="https://rust.godbolt.org/z/qWTYse">godbolt</a>)</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code><span class="nv nv-Anonymous">!6</span> <span class="p">=</span> <span class="k">distinct</span> <span class="nv">!DISubprogram</span><span class="p">(</span><span class="nl">name:</span> <span class="s">"foo"</span><span class="p">,</span> <span class="nl">linkageName:</span> <span class="s">"_ZN7example3foo17h1e2e1503bf917904E"</span><span class="p">,</span> <span class="nl">scope:</span> <span class="nv nv-Anonymous">!8</span><span class="p">,</span> <span class="nl">file:</span> <span class="nv nv-Anonymous">!7</span><span class="p">,</span> <span class="nl">line:</span> <span class="m">5</span><span class="p">,</span> <span class="nl">type:</span> <span class="nv nv-Anonymous">!9</span><span class="p">,</span> <span class="nl">scopeLine:</span> <span class="m">5</span><span class="p">,</span> <span class="nl">flags:</span> <span class="err">DIFlagPrototyped</span><span class="p">,</span> <span class="nl">spFlags:</span> <span class="err">DISPFlagDefinition</span> <span class="err">|</span> <span class="err">DISPFlagOptimized</span><span class="p">,</span> <span class="nl">unit:</span> <span class="nv nv-Anonymous">!3</span><span class="p">,</span> <span class="nl">templateParams:</span> <span class="nv nv-Anonymous">!5</span><span class="p">,</span> <span class="nl">retainedNodes:</span> <span class="nv nv-Anonymous">!12</span><span class="p">)</span>

<span class="nv nv-Anonymous">!13</span> <span class="p">=</span> <span class="nv">!DILocalVariable</span><span class="p">(</span><span class="nl">name:</span> <span class="s">"first_var"</span><span class="p">,</span> <span class="nl">arg:</span> <span class="m">1</span><span class="p">,</span> <span class="nl">scope:</span> <span class="nv nv-Anonymous">!6</span><span class="p">,</span> <span class="nl">file:</span> <span class="nv nv-Anonymous">!7</span><span class="p">,</span> <span class="nl">line:</span> <span class="m">5</span><span class="p">,</span> <span class="nl">type:</span> <span class="nv nv-Anonymous">!11</span><span class="p">)</span>
<span class="nv nv-Anonymous">!14</span> <span class="p">=</span> <span class="nv">!DILocalVariable</span><span class="p">(</span><span class="nl">name:</span> <span class="s">"first_var"</span><span class="p">,</span> <span class="nl">scope:</span> <span class="nv nv-Anonymous">!15</span><span class="p">,</span> <span class="nl">file:</span> <span class="nv nv-Anonymous">!7</span><span class="p">,</span> <span class="nl">line:</span> <span class="m">8</span><span class="p">,</span> <span class="nl">type:</span> <span class="nv nv-Anonymous">!11</span><span class="p">,</span> <span class="nl">align:</span> <span class="m">4</span><span class="p">)</span>
<span class="nv nv-Anonymous">!15</span> <span class="p">=</span> <span class="k">distinct</span> <span class="nv">!DILexicalBlock</span><span class="p">(</span><span class="nl">scope:</span> <span class="nv nv-Anonymous">!6</span><span class="p">,</span> <span class="nl">file:</span> <span class="nv nv-Anonymous">!7</span><span class="p">,</span> <span class="nl">line:</span> <span class="m">8</span><span class="p">,</span> <span class="nl">column:</span> <span class="m">9</span><span class="p">)</span>
</code></pre></div>
<p>But I am not an expert.</p>



<a name="228840950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Impacts%20of%20optimization%20on%20debug%20information/near/228840950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Impacts.20of.20optimization.20on.20debug.20information.html#228840950">(Mar 04 2021 at 20:04)</a>:</h4>
<p>Yeah but i wouldn't guarantee that given that the debug info in question Is orphaned effectively.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>